package com.aoxin.controller.course.systemCourse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.aoxin.mapper.SlfClassroomEmployMapper;
import com.aoxin.mapper.SlfUserCourseConditionMapper;
import com.aoxin.mapper.course.systemCourse.SchoolCoursePageDao;
import com.aoxin.mapper.course.webCourse.MyCourseMapper;
import com.aoxin.mapper.course.webCourse.SlfChildrenMapper;
import com.aoxin.mapper.opm.SlfClassroomMapper;
import com.aoxin.model.SlfClassroomEmploy;
import com.aoxin.model.SlfUserCourseCondition;
import com.aoxin.model.course.SchoolCoursePage;
import com.aoxin.model.dto.SelectTreeDto;
import com.aoxin.service.inter.course.systemCourse.SchoolCoursePageService;
import com.aoxin.util.AjaxRes;
import com.aoxin.util.Const;
import com.aoxin.util.ThreadLocalUtil;
import com.aoxin.util.enumerate.OperateType;
import com.aoxin.util.log.LogInfo;

@RestController
@ResponseBody
@RequestMapping("/web/schoolCoursePage/")
public class SchoolCoursePageController {
	@Autowired
	private SchoolCoursePageService slfSchoolCoursePageService;
    @Autowired
    private MyCourseMapper myCourseMapper;
    @Autowired
    private SlfClassroomEmployMapper slfClassroomEmployMapper;
    @Autowired
    private SlfUserCourseConditionMapper slfUserCourseConditionMapper;
    @Autowired
	private SchoolCoursePageDao schoolCoursePageDao;
	@Autowired
	SlfChildrenMapper slfchildrenmapper;
	@Autowired
	private SlfClassroomMapper slfClassroomMapper;
    
	/**
	 * 查询某个学校的课程包
	 * 
	 * @param slfSchoolCoursePage
	 * @return
	 */
	@LogInfo(operateType = OperateType.QUERY, content = "查询某个学校的课程包")
	@PostMapping("getSchoolCoursePage")
	public AjaxRes getSchoolCoursePage(SchoolCoursePage slfSchoolCoursePage, AjaxRes res) {
		try {
			if(slfSchoolCoursePage.getSchoolId()==null ||slfSchoolCoursePage.getSchoolId()==""){
				String schoolId = String.valueOf(ThreadLocalUtil.getEmployee().getSchoolId());
				slfSchoolCoursePage.setSchoolId(schoolId);
			}
			List<SchoolCoursePage> schoolCoursePage = slfSchoolCoursePageService.getSchoolCoursePage(slfSchoolCoursePage);
			res.setSucceed(schoolCoursePage);
		} catch (Exception e) {
			res.setFailMsg(e.getMessage());
		}
		return res;
	}

	/**
	 * 新建某个学校的课程包
	 * 
	 * @param schoolCoursePage
	 * @return
	 */
	@LogInfo(operateType = OperateType.INSERT, content = "新增某个学校的课程包")
	@PostMapping("insertSchoolCoursePage")
	public AjaxRes insertSchoolCoursePage(@RequestParam("coursePageIds") List<Integer> coursePageIds, AjaxRes res) {
		try {
			slfSchoolCoursePageService.insertSchoolCoursePage(coursePageIds);
			res.setSucceedMsg(Const.SAVE_SUCCEED);
		} catch (Exception e) {
			res.setFailMsg(e.getMessage());
		}
		return res;
	}

	/**
	 * 修改某个学校的课程包
	 * 
	 * @param schoolCoursePage
	 * @return
	 */
	@LogInfo(operateType = OperateType.UPDATE, content = "修改某个学校的课程包")
	@PutMapping("updateSchoolCoursePage")
	public AjaxRes updateSchoolCoursePage(SchoolCoursePage schoolCoursePage, AjaxRes res) {
		try {
			 slfSchoolCoursePageService.updateSchoolCoursePage(schoolCoursePage);
			res.setSucceedMsg(Const.SAVE_SUCCEED);
		} catch (Exception e) {
			res.setFailMsg(e.getMessage());
		}
		return res;
	}

	/**
	 * 课程包置顶
	 * 
	 * @param schoolCoursePageId
	 *            学校课程包
	 */
	public AjaxRes setSchoolCoursePageTop(Integer schoolCoursePageId, AjaxRes res) {
		try {
			this.slfSchoolCoursePageService.setSchoolCoursePageTop(schoolCoursePageId);
			res.setSucceedMsg(Const.UPDATE_SUCCEED);
		} catch (Exception e) {
			res.setFailMsg(e.getMessage());
		}
		return res;
	}

	/**
	 * 查询课程包树形下拉
	 * 
	 * @param res
	 * @return
	 */
	@PutMapping("getCoursePageTreeList")
	public AjaxRes getCoursePageTreeList(AjaxRes res) {
		try {
			List<SelectTreeDto> selectTreeDto = slfSchoolCoursePageService.getCoursePageTreeList();
			res.setSucceed(selectTreeDto, Const.DATA_SUCCEED);
		} catch (Exception e) {
			res.setFailMsg(e.getMessage());
		}
		return res;
	}

	/**
	 * 根据课表id查询课程包
	 * 
	 * @param ajaxRes
	 * @param syllabusId
	 * @return
	 */
	@RequestMapping("queryCoursePageById")
	public AjaxRes queryCoursePageById(AjaxRes ajaxRes, Integer syllabusId, String type) {
		try {
			List<SchoolCoursePage> schoolCoursePageList = this.slfSchoolCoursePageService.getSchoolCoursePage(syllabusId, type);
			ajaxRes.setSucceed(schoolCoursePageList, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
	/**
	 * 查询某个学校的课程包
	 * 
	 * @param slfSchoolCoursePage
	 * @return
	 */
	@LogInfo(operateType = OperateType.QUERY, content = "查询某个学校的课程包")
	@PostMapping("getSchoolCoursePageMoney")
	public AjaxRes getSchoolCoursePageMoney(SchoolCoursePage slfSchoolCoursePage, AjaxRes res) {
		try {
			List<SchoolCoursePage> schoolCoursePage = slfSchoolCoursePageService.getSchoolCoursePage(slfSchoolCoursePage);
			
			boolean childrenBoolean=false;
			if(ThreadLocalUtil.getUser()!=null){
				//获取孩子信息
			List<Integer> listChildrenId=slfchildrenmapper.getChildrenId(ThreadLocalUtil.getUser().getUserId());
			if(listChildrenId.size()>0){
				childrenBoolean=true;
			}
			}
			if(ThreadLocalUtil.getUser()!=null && childrenBoolean){
				for (SchoolCoursePage schoolCoursepage : schoolCoursePage) {
					//查询用户是否已经购买该课程包
					  int count = this.myCourseMapper.getCountMyCourse(ThreadLocalUtil.getChildren().getChildrenId(), schoolCoursepage.getSchoolCoursePageId());
		              if(count == 1){
		            	  schoolCoursepage.setIsBuy("Y");
		              }
				}
			}
			res.setSucceed(schoolCoursePage);
		} catch (Exception e) {
			res.setFailMsg(e.getMessage());
		}
		return res;
	}
	/**
	 * 课表管理(课表查询主)
	 * 
	 * @param 
	 * 
	 * @return List<>
	 */
	@LogInfo(operateType = OperateType.QUERY, content = "查询占课情况")
	@RequestMapping("getSchoolCourseSyllabus")
	public AjaxRes getSchoolCourseSyllabus(String date, AjaxRes res){
		try {
		//	Integer week = DateWay.getDayofweek(date);
		//	String wk = DateWay.WeekDay(week);
			List<SlfClassroomEmploy> slfClassroomEmploys = new ArrayList<SlfClassroomEmploy>();
			String[]  timeQuantums = Const.TIMESECTION;
			for(String timeQuantum : timeQuantums){
			//先找B,再找C(1B;2C;3无)
		  //String weekTime = wk + "," + timeQuantum;
			String endTime = timeQuantum.substring(timeQuantum.length()-5,timeQuantum.length());
			endTime += ":00";
			endTime = date + " " + endTime;
			SlfClassroomEmploy slfClassroomEmploy = slfClassroomEmployMapper.getIsEmployClassroomMasseage(endTime);	
			if(slfClassroomEmploy == null){
			//判断C类
			String startTime = timeQuantum.substring(0,5);
			startTime += ":00";
			startTime = date + " " + startTime;
			SlfClassroomEmploy ClassroomEmploy = slfClassroomEmployMapper.getIsEmployClassroomCMasseage(startTime);
			if(ClassroomEmploy != null){
			SlfClassroomEmploy ClassroomEmployC = new SlfClassroomEmploy();
			ClassroomEmployC.setEmployTime(timeQuantum);
			ClassroomEmployC.setType(2);
			ClassroomEmployC.setEndTime(ClassroomEmploy.getEndTime());
			slfClassroomEmploys.add(ClassroomEmployC);
			}else{
			SlfClassroomEmploy ClassroomEmployNo = new SlfClassroomEmploy();
			ClassroomEmployNo.setEmployTime(timeQuantum);
			ClassroomEmployNo.setType(3);
			slfClassroomEmploys.add(ClassroomEmployNo);
			}
			}else{
			//被B类课程占用
			SlfClassroomEmploy ClassroomEmployB = new SlfClassroomEmploy();
			ClassroomEmployB.setEndTime(slfClassroomEmploy.getEndTime());
			ClassroomEmployB.setType(1);
			ClassroomEmployB.setEmployTime(timeQuantum);
			slfClassroomEmploys.add(ClassroomEmployB);
			  }
			}
			res.setSucceed(slfClassroomEmploys);
		} catch (Exception e) {
			res.setFailMsg(e.getMessage());
		}
		return res;
	}
	
	@LogInfo(operateType = OperateType.QUERY, content = "查询课程表")
	@RequestMapping("getSchoolCourseClassroom")
	public AjaxRes getClassroomTable(String endTime,Integer classroomId,AjaxRes res,Integer type){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("employTime",endTime);
		map.put("classroomId",classroomId);
		try {
			//B类
			List<SlfUserCourseCondition> slfUserCourseCondition = slfUserCourseConditionMapper.getCourseConditionTimeClassroom(map);
			//C类
			List<SchoolCoursePage> SchoolCoursePage = schoolCoursePageDao.getCourseConditionTimeCClassroom(map);
			for(int i = 0 ; i < SchoolCoursePage.size();i++){
				SlfUserCourseCondition model = new SlfUserCourseCondition();
				model.setClassroomId(SchoolCoursePage.get(i).getClassroomId());
				model.setClassroomName(SchoolCoursePage.get(i).getClassroomName());
				slfUserCourseCondition.add(model);
			}
			res.setSucceed(slfUserCourseCondition);
		} catch (Exception e) {
			res.setFailMsg(e.getMessage());
		}
		return res;
	}
	
	@LogInfo(operateType = OperateType.QUERY, content = "查询课程表")
	@RequestMapping("getSchoolCourseTable")
	public AjaxRes getSchoolCourseTable(String endTime,Integer classroomId,AjaxRes res,Integer type){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("employTime",endTime);
		map.put("classroomId",classroomId);
		try {
			//B类
			if(type == 1){
			List<SlfUserCourseCondition> slfUserCourseCondition = slfUserCourseConditionMapper.getCourseConditionTime(map);
			for(int i = 0;i < slfUserCourseCondition.size();i++){
				slfUserCourseCondition.get(i).setTeachName(slfClassroomMapper.getClassroomTeachName(slfUserCourseCondition.get(i).getClassroomId()));
			}
			res.setSucceed(slfUserCourseCondition);
			}else{
			//C类
			List<SchoolCoursePage> SchoolCoursePage = schoolCoursePageDao.getCourseConditionTimeC(map);
			for(int i = 0 ; i < SchoolCoursePage.size();i++){
				SchoolCoursePage.get(i).setUserName(SchoolCoursePage.get(i).getChildrenName());
				SchoolCoursePage.get(i).setName(SchoolCoursePage.get(i).getCourseName());
				SchoolCoursePage.get(i).setTeachName(slfClassroomMapper.getClassroomTeachName(SchoolCoursePage.get(i).getClassroomId()));  
			}
			res.setSucceed(SchoolCoursePage);
			}
		} catch (Exception e) {
			res.setFailMsg(e.getMessage());
		}
		return res;
	}
}
